import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Author limingcheng
 * @Description 线程池工具
 * @Date 2019/12/12 15:38
 * @Version v1.0
 **/
public class ThreadPoolUtil {
    private ExecutorService executorService;


    /**
     * @author limingcheng
     * @Description 创建工具对象
     * @Date 2019/12/12 15:55
     * @Param [executorService]
     * @return com.yxzq.stock.order.common.thread.ThreadPoolUtil
     **/
    public  static ThreadPoolUtil create(ExecutorService executorService){
        ThreadPoolUtil threadPoolUtil = new ThreadPoolUtil();
        threadPoolUtil.executorService = executorService;
        return threadPoolUtil;
    }
    
    /**
    * @author limingcheng
    * @Description 异步获取数据
    * @Date 2019/12/12 15:51
    * @Param [executorService, callable]
    * @return java.util.concurrent.Future<T>
    **/
    public  <T> Future<T>  submitFuture(Callable<T> callable){
        if(!executorService.isShutdown()){
            return  executorService.submit(callable);
        }
        throw new RuntimeException("线程执行器已关闭");
    }

    /**
     * 异步执行
     *
     * @author wenwen.li@usmarthk.com
     * @date 2020/9/25 14:36
     * @param command
     */
    public void execute(Runnable command) {
        if (!executorService.isShutdown()) {
            executorService.execute(command);
        }
    }

    /**
    * @author limingcheng
    * @Description 获取结果future
    * @Date 2019/12/12 16:03
    * @Param [futureList]
    * @return java.util.List<T>
    **/
    public  <T> List<T> getFutureResult(List<Future<T>> futureList){
        List<T> list = new ArrayList<>(1024);
        for (Future<T> future : futureList) {
            try {
                T result = future.get();
                if (result != null) {
                    list.add(result);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return list;
    }


    public static void waitUntilTerminate(final ExecutorService executorService, final int timeout) {
        if(executorService == null || executorService.isShutdown()){
            return;
        }
        try {
            executorService.shutdown();
            if (!executorService.awaitTermination(timeout, TimeUnit.SECONDS)) { //超时后直接关闭
                executorService.shutdownNow();
            }
        } catch (InterruptedException e) { //awaitTermination 出现中断异常也将触发关闭
            executorService.shutdownNow();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        ThreadPoolUtil threadPoolUtil = ThreadPoolUtil.create(new ThreadPoolExecutor(32, 64, 5, TimeUnit.MINUTES,
                new LinkedBlockingDeque<>(1000),new ThreadPoolExecutor.CallerRunsPolicy()));
        List<Future<Integer>> futureList = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            futureList.add(threadPoolUtil.submitFuture(() -> {
                Thread.sleep(200);
                return 1;
            }));
        }
        List<Integer> integerList = threadPoolUtil.getFutureResult(futureList);
        long result = System.currentTimeMillis() - start;
        System.out.println("耗时"+result+"毫秒");
        System.out.println("integerList:"+integerList.size());

        Thread.sleep(600000L);
    }

    public ExecutorService getExecutorService() {
        return executorService;
    }
}
